import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_date_type.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/values/gaps.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/generated/assets.dart';

import 'today_controller.dart';

///今日警情
class TodayPage extends StatelessWidget {
  TodayPage({super.key});

  final controller = Get.put(TodayController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: '今日警情',
      ),
      body: GetBuilder(
          init: controller,
          builder: (_) {
            return Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: CustomEasyRefreshList<AlarmRespEntity>(
                dataLoad: controller.theFeignEmergencyPlansPage,
                // enableLoad: false,
                key: controller.globalKey,
                itemBuilder: (AlarmRespEntity item, int index) {
                  return CustomTitleChartPage(
                    contentPadding:
                        EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
                    title: item.companyName,
                    titleRightWidget: getStatueView(item.disposalStatus??''),
                    child: Stack(
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Expanded(
                                  child: _getTextView(
                                    title:
                                        '${CustomDateType.getDataType(item.dataType)}级别:',
                                    value: CustomDateType.getAlarmType(
                                        item.alarmLevel ?? 0,
                                        item.dataType ?? 0,
                                        isShow: true),
                                  ),
                                ),
                                Expanded(
                                   child: _getTextView(title: '${CustomDateType.getDataType(item.dataType)}值:',
                                        value: '${item.val??''}')),
                              ],
                            ),
                            _getTextView(
                              title: '系统-类别:',
                              value: item.categoryZh??'',
                            ),
                            _getTextView(
                                title: '报警时长:',
                                value: '${item.alarmTimeMin}分钟'),
                            _getTextView(
                                title: '报警时间:',
                                value: '${item.alarmStartTime}'.millTS2Str()),
                          ],
                        ),
                        Positioned(
                          right: 0,
                          bottom: 0,
                          child: Stack(
                            alignment: Alignment.center,
                            children: [
                              SvgPicture.asset(
                                Assets.svgIconEnter,
                                fit: BoxFit.cover,
                              ),
                              Icon(
                                Icons.arrow_forward,
                                size: 20.w,
                                color: Colors.white,
                              ).marginOnly(left: 7.w, top: 6.w),
                            ],
                          ),
                        ),
                        Positioned(
                          right: 44,
                          bottom: 0,
                          child: Visibility(
                            visible: item.supervisionStatusStr != '0', //0、无操作按钮权限
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                SvgPicture.asset(
                                  'assets/svg/icon_enter_green.svg',
                                  fit: BoxFit.cover,
                                ),
                                Text(
                                  item.supervisionStatusStr == '1'||item.supervisionStatusStr == '2'? '督办' :  item.supervisionStatusStr == '3'?'派发':'处置',
                                  style: TextStyle(
                                    fontSize: 14.sp,
                                    height: 1.2,
                                    fontWeight: FontWeight.w400,
                                    color: Colors.white,
                                  ),
                                )
                              ],
                            ).clickOnce(() {
                              controller.onClickOperateDialog(item);
                            }),
                          ),
                        ),
                      ],
                    ),
                  ).clickOnce(() {
                    controller.goDetails(item);
                  });;
                },
              ),
            );
          }),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }


  /// 处置进度状态（1未处置，2.超时未处置，3.处置中，4.超时处置中，5.及时完成，6.超时完成）
  Widget getStatueView(String status) {
    String name = '';
    Color? bgColor;
    switch (status) {
      case '1':
        name = '未处置';
        break;
      case '2':
        name = '超时未处置';
        bgColor = const Color(0xFFFE9843);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '3':
        name = '处置中';
        bgColor = const Color(0xFFFE9843);
        break;
      case '4':
        name = '超时处置中';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '5':
        name = '及时完成';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
      case '6':
        name = '超时完成';
        bgColor = const Color(0xFF63C320);
        return Stack(
          alignment: Alignment.centerLeft,
          children: [
            SvgPicture.asset(
              height: 12.w,
              width: 12.w,
              Assets.svgBgStatue,
              fit: BoxFit.cover,
              colorFilter:  ColorFilter.mode(bgColor, BlendMode.srcIn),
            ),
            Positioned(
              left: 16.w,
              child: Text(
                name,
                style: TextStyle(
                  fontSize: 10.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xFF3F425B),
                ),
              ),
            ),
          ],
        );
    }
    return SizedBox(
      child: Stack(
        alignment: Alignment.centerRight,
        children: [
          SvgPicture.asset(
            Assets.svgBgStatue,
            height: 12.w,
            width: 12.w,
            fit: BoxFit.cover,
            colorFilter: bgColor == null ? null : ColorFilter.mode(bgColor, BlendMode.srcIn),
          ),
          Text(
            name,
            style: TextStyle(
              fontSize: 14.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF3F425B),
            ),
          ),
        ],
      ),
    );
  }
}
